/**
 * 
 */
package edu.arizona.eller.mis.liferoulette;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import edu.arizona.eller.mis.liferoulette.db.dao.ActivityDAO;
import edu.arizona.eller.mis.liferoulette.entity.ActivityBean;

import android.content.Context;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class HistoryAdapter extends BaseAdapter {

	//private Context c = null;
	private LayoutInflater layoutInflator;
	private List<ActivityBean> data;
	public HistoryAdapter(Context c) {
		//this.c = c;
		layoutInflator = LayoutInflater.from(c);
		ActivityDAO dao = new ActivityDAO(c);
		data = dao.getActivities();
		dao.closeDAO();
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int i, View currentView, ViewGroup arg2) {
		Map<Integer, TextView> viewMapT1 = null;//date
		Map<Integer, TextView> viewMapT2 = null;//place
		Map<Integer,ImageView> viewMapImage = null;
		
		if (currentView == null) {
			currentView = layoutInflator.inflate(R.layout.history_row, null);
			viewMapT1 = new HashMap<Integer, TextView>();
			viewMapT2 = new HashMap<Integer, TextView>();
			viewMapImage = new HashMap<Integer,ImageView>();
			viewMapImage.put(R.id.historyIcon, (ImageView)currentView.findViewById(R.id.historyIcon));
			viewMapT1.put(R.id.historyDate, (TextView)currentView.findViewById(R.id.historyDate));
			viewMapT2.put(R.id.historyPlace, (TextView)currentView.findViewById(R.id.historyPlace));
			//viewMap.put(R.id.historyComments, (TextView)currentView.findViewById(R.id.historyComments));
		} else {
			viewMapT1 = (HashMap<Integer, TextView>)currentView.getTag(R.id.historyDate);
			viewMapT2 = (HashMap<Integer, TextView>)currentView.getTag(R.id.historyPlace);
			viewMapImage = (HashMap<Integer, ImageView>)currentView.getTag(R.id.historyIcon);
		}
		
		ActivityBean a = data.get(i);
		try {
			JSONObject currentItem = new JSONObject(a.getActivity());
			
			String imageUrl = currentItem.getString("icon");
			viewMapImage.get(R.id.historyIcon).setImageBitmap(BitmapFactory.decodeStream((InputStream) new URL(imageUrl).getContent()));
			viewMapT1.get(R.id.historyDate).setText(currentItem.getInt("rating") + " Stars");
			viewMapT2.get(R.id.historyPlace).setText(currentItem.getString("name"));
			//viewMap.get(R.id.historyComments).setText(currentItem.getString("vicinity"));
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		currentView.setTag(R.id.historyDate,viewMapT1);
		currentView.setTag(R.id.historyPlace,viewMapT2);
		currentView.setTag(R.id.historyIcon,viewMapImage);
		return currentView;
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int i) {
		return data.get(i);
	}

	@Override
	public long getItemId(int arg0) {
		return 0;
	}
}
